package com.jy.wms.controller;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.pojo.WmsOrderReport;
import com.jy.wms.service.WmsOrderReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelStyleType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author lh
 * @Date 2018/3/21 0021
 */

@RestController
@RequestMapping("/wmsorderreport")
@Api(value = "/wmsorderreport", description = "关于报表操作(lh)")
public class WmsOrderReportController  extends BaseController<WmsOrderReportService, WmsOrderReport> {
    @PostMapping(value = "/list")
    @ApiOperation(value = "已发货订单产品数量各地区分布比例 查询", response = JSONObject.class, notes = "已发货订单产品数量各地区分布比例 查询")
    public @ResponseBody
    JSONObject getWmsArtificialReplenishment(@RequestBody Map map, HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        /*String ckid="2";*/
;        //entity.setCkid(ckid);
        map.put("ckid",ckid);
        List<Map> list = baseService.baseSelectPage(map);
         Integer total= baseService.getAllGoodsCount(map);
        //return JSONArray.fromObject(list).toString();
        return SetStatus.setSearchType(JSONArray.fromObject(list).toString(),  total);
    }




    @ApiOperation(value = "导出已发货订单产品数量各地区分布比例", response = JSONObject.class, notes = "导出已发货订单产品数量各地区分布比例")
    @PostMapping(value = "getOutPutList")
    public JSONObject getOutPutList(@RequestBody Map map,HttpServletResponse response,HttpSession session) throws IOException {

    TemplateExportParams params = new TemplateExportParams("import/已发货订单产品数量导出模板.xls",true);
    params.setHeadingRows(2);
    params.setHeadingStartRow(2);
    params.setStyle(ExcelStyleType.BORDER.getClazz());
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("已发货订单产品数量各地区分布比例", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
         UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        //entity.setCkid(ckid);
        map.put("ckid",ckid);
        List<Map> list = baseService.getOutPutList((List)map.get("ids"),map);
        Map maps=new HashedMap();
        maps.put("maplist",list);
        Workbook workbook = ExcelExportUtil.exportExcel(params, maps);
        workbook.write(response.getOutputStream());
       /* File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:/excel/已发货订单产品数量各地区分布比例.xls");
        workbook.write(fos);
        fos.close();*/
        return null;
    }





    @PostMapping(value = "/getSaleMoney")
    @ApiOperation(value = "已发货订单产品销售额各地区分布比例 查询", response = JSONObject.class, notes = "已发货订单产品销售额各地区分布比例 查询")
    public @ResponseBody
    JSONObject getSaleMoney(@RequestBody  Map map, HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
       /* String ckid="2";*/
        //entity.setCkid(ckid);
        map.put("ckid",ckid);
        List<Map> list = baseService.getSaleMoney(map);
        Integer total= baseService.getAllGoodsCount(map);
        return SetStatus.setSearchType(JSONArray.fromObject(list).toString(),  total);
    }

    @ApiOperation(value = "导出已发货订单产品销售额各地区分布比例", response = JSONObject.class, notes = "导出已发货订单产品销售额各地区分布比例")
    @PostMapping(value = "saleMoneyOutPut")
    public void saleMoneyOutPut(@RequestBody Map map,HttpServletResponse response, HttpSession session) throws IOException {
        TemplateExportParams params = new TemplateExportParams("import/已发货订单产品数量导出模板.xls",true);
        params.setHeadingRows(2);
        params.setHeadingStartRow(2);
        params.setStyle(ExcelStyleType.BORDER.getClazz());

        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("已发货订单产品数量各地区分布比例", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        //entity.setCkid(ckid);
        map.put("ckid",ckid);
        List<Map> list = baseService.saleMoneyOutPut((List)map.get("ids"),map);
        Map maps=new HashedMap();
        maps.put("maplist",list);

        Workbook workbook = ExcelExportUtil.exportExcel(params, maps);
        workbook.write(response.getOutputStream());
      /*  File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:/excel/已发货订单产品销售额各地区分布比例.xls");
        workbook.write(fos);
        fos.close();*/

    }










/*

    @PostMapping(value = "/getProductMoney")
    @ApiOperation(value = " 已发货订单产品销售额各地区分布比例（分类） 查询", response = JSONObject.class, notes = " 已发货订单产品销售额各地区分布比例（分类） 查询")
    public @ResponseBody
    JSONObject getProductMoney(@RequestBody WmsOrderReport entity, HttpSession session) {
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        entity.setCkid(ckid);
        List<Map> list = baseService.getOrderProductTotal(entity);

        return SetStatus.setSearchType(JSONArray.fromObject(list).toString(),  list.size());
    }


    @ApiOperation(value = "导出已发货订单产品销售额各地区分布比例（分类）", response = JSONObject.class, notes = "导出已发货订单产品销售额各地区分布比例（分类）")
    @GetMapping(value = "getOutPutListMoney")
    public void getOutPutListMoney(HttpSession session) throws IOException {
         WmsOrderReport entity =new WmsOrderReport();
        TemplateExportParams params = new TemplateExportParams("import/已发货订单产品数量导出模板.xls",true);
        params.setHeadingRows(2);
        params.setHeadingStartRow(2);
        params.setStyle(ExcelStyleType.BORDER.getClazz());
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        entity.setCkid(ckid);
        List<Map> list = baseService.getOutPutList(entity);
        Map maps=new HashedMap();
        maps.put("maplist",list);
        Workbook workbook = ExcelExportUtil.exportExcel(params, maps);
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:/excel/直销系统已发货订单产品销售额各地区分布比例（分类）.xls");
        workbook.write(fos);
        fos.close();

    }
*/

}
